/**
 * Created by wenguangjia on 2017/8/25.
 */
(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('transferCountController', transferCountController);
    transferCountController.$inject = ['$scope', 'searchService', '$sce', 'ACTIVE', 'kibanaPartition', 'transerCountService'];

    function transferCountController($scope, searchService, $sce, active, kibanaPartition, transerCountService) {
        var callServiceEchart = echarts.init(angular.element('#callServiceEchart')[0]);
        var teleServiceEchart = echarts.init(angular.element('#teleServiceEchart')[0]);
        var batchServiceEchart = echarts.init(angular.element('#batchServiceEchart')[0]);
        var asyncRouteServiceEchart = echarts.init(angular.element('#asyncRouteServiceEchart')[0]);
        var callFrameServiceEchart = echarts.init(angular.element('#callFrameServiceEchart')[0]);
        var uidServiceEchart = echarts.init(angular.element('#uidServiceEchart')[0]);


        var currentTime = new Date();
        $scope.partition = '全部';
        $scope.show = [];
        $scope.partitionName = '';
        $scope.timeType = '1d';
        $scope.startTime = getToday00Clock().Format("yyyy-MM-ddThh:mm:ss.S");
        $scope.endTime = getToday11Clock().Format("yyyy-MM-ddThh:mm:ss.S");
        $scope.code = "";
        // sceTrust('all');

        /**
         * get partition data
         */
        searchService.getPartition({}).then(
            function (resp) {
                $scope.partitionSource = toTree(resp.data);

                $scope.partitionName = kibanaPartition;
                angular.forEach(resp.data, function (item) {
                    $scope.show.push(item.ename);
                });
                $scope.show.push('all');
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            }, function (err) {
            }
        );


        $scope.selectShow = function (k) {
            if (k == 'all') {
                $scope.partition = '全部';
                $scope.code = "";
                $scope.partitionName = kibanaPartition;
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
                //   sceTrust('all');
            } else {
                angular.forEach($scope.show, function (item) {
                    if (item == k.ename) {
                        $scope.partition = k.fname + "-" + k.cname;
                        $scope.partitionName = k.ename;
                        //sceTrust(item);
                    }
                });
                $scope.code = k.code;
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            }

            $scope.win.close();
        }


        $scope.select = function () {
            if ($scope.timeType == '1d') {
                $scope.startTime = getToday00Clock().Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.endTime = getToday11Clock().Format("yyyy-MM-ddThh:mm:ss.S");
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            } else if ($scope.timeType == '1h') {
                currentTime.setHours(currentTime.getHours() - 1);
                $scope.startTime = (currentTime.Format("yyyy-MM-ddThh:mm:ss.S"));
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            } else if ($scope.timeType == '4h') {
                currentTime.setHours(currentTime.getHours() - 4);
                $scope.startTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            } else if ($scope.timeType == '12h') {
                currentTime.setHours(currentTime.getHours() - 12);
                $scope.startTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                queryCallServiceTop10();
                queryTeleServiceTop10();
                queryUidServiceTop10();
                queryBatchServiceTop10();
                queryAsyncRouteServiceTop10();
                queryCallFrameServiceTop10();
            }
        }


        function queryCallServiceTop10() {
            $scope.logmsg = "4. service end.";
            transerCountService.queryCallServiceTop10($scope.partitionName, $scope.logmsg, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {

                    callServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function callServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var callServiceOption = {
                    color: ['#DAA05D'],
                    title: {
                        text: partition + "服务调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        textStyle: {
                            color: "#636363"
                        },
                        data: ['服务调用次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >服务调用次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>服务名:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '服务名',
                            nameLocation: "center",
                            nameGap: 105,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '服务调用次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                callServiceEchart.setOption(callServiceOption, true);
            } else {
                var callServiceOption = {
                    title: {
                        text: partition + "服务调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                callServiceEchart.setOption(callServiceOption, true);
            }
            ;
        }

        function queryTeleServiceTop10() {
            $scope.logmsg = "6. service end.";
            transerCountService.queryCallServiceTop10($scope.partitionName, $scope.logmsg, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {
                    teleServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function teleServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var teleServiceOption = {

                    color: ['#BC52BC'],
                    title: {
                        text: partition + "电文service调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        data: ['电文service调用次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >电文service调用次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>电文service名:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '电文service名',
                            nameLocation: "center",
                            nameGap: 105,

                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '电文service调用次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                teleServiceEchart.setOption(teleServiceOption, true);
            } else {
                var teleServiceOption = {
                    title: {
                        text: partition + "电文service调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                teleServiceEchart.setOption(teleServiceOption, true);
            }
            ;
        }

        function queryUidServiceTop10() {

            transerCountService.queryUidServiceTop10($scope.partitionName, $scope.logmsg, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {
                    uidServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function uidServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var uidServiceOption = {

                    color: ['#006E8A'],
                    title: {
                        text: partition + "用户调用服务Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        data: ['用户调用次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >用户调用次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>用户工号:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '用户工号',
                            nameLocation: "center",
                            nameGap: 60,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '用户调用次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                uidServiceEchart.setOption(uidServiceOption, true);
            } else {
                var uidServiceOption = {
                    title: {
                        text: partition + "用户调用服务Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                uidServiceEchart.setOption(uidServiceOption, true);
            }
            ;
        }

        function queryBatchServiceTop10() {

            transerCountService.queryBatchServiceTop10($scope.partitionName, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {
                    batchServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function batchServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var batchServiceOption = {

                    color: ['#6F87D8'],
                    title: {
                        text: partition + "批作业执行Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        data: ['批作业执行次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >批作业执行次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>批作业名:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '批作业名',
                            nameLocation: "center",
                            nameGap: 170,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '批作业执行次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                batchServiceEchart.setOption(batchServiceOption, true);
            } else {
                var batchServiceOption = {
                    title: {
                        text: partition + "批作业执行Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                batchServiceEchart.setOption(batchServiceOption, true);
            }
            ;
        }

        function queryAsyncRouteServiceTop10() {
            transerCountService.queryAsyncRouteServiceTop10($scope.partitionName, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {
                    asyncRouteServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function asyncRouteServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var asyncRouteServiceOption = {

                    color: ['#9E3533'],
                    title: {
                        text: partition + "异步路由电文号Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        data: ['电文调用次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >电文调用次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>电文号:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '电文号',
                            nameLocation: "center",
                            nameGap: 70,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '电文调用次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                asyncRouteServiceEchart.setOption(asyncRouteServiceOption, true);
            } else {
                var asyncRouteServiceOption = {
                    title: {
                        text: partition + "异步路由电文号Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                asyncRouteServiceEchart.setOption(asyncRouteServiceOption, true);
            }
            ;
        }

        function queryCallFrameServiceTop10() {
            transerCountService.callFrameServiceTop10($scope.partitionName, $scope.startTime, $scope.endTime, $scope.code, active).then(
                function (resp) {
                    callFrameServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function callFrameServiceAnalysis(data, partition) {

            var xTime = [];
            var yCount = [];
            if (data.length != 0) {
                angular.forEach(data, function (item) {
                        xTime.push(item.service);
                        yCount.push(item.count);
                    }
                );
                var callFrameServiceOption = {

                    color: ['#663DB8'],
                    title: {
                        text: partition + "画面调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left: "right",
                        data: ['画面访问次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >画面访问次数:" + "&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>画面代码:" + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '画面代码',
                            nameLocation: "center",
                            nameGap: 90,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                rotate: 45
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            //name:"",
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '画面访问次数',
                            type: 'bar',
                            barWidth: '60%',
                            barMinHeight: 1,
                            data: yCount
                        }
                    ]

                }
                callFrameServiceEchart.setOption(callFrameServiceOption, true);
            } else {
                var callFrameServiceOption = {
                    title: {
                        text: partition + "画面调用Top10",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    noDataLoadingOption: {
                        text: '暂无数据',
                        effect: 'bubble',
                        effectOption: {
                            effect: {
                                n: 0
                            }
                        }
                    }
                };
                callFrameServiceEchart.setOption(callFrameServiceOption, true);
            }
            ;
        }


        //图表自适应
        window.onresize = function () {
            callServiceEchart.resize();
            teleServiceEchart.resize();
            batchServiceEchart.resize();
            asyncRouteServiceEchart.resize();
            callFrameServiceEchart.resize();
            uidServiceEchart.resize();

        }
    }


})();
